package com.cloudkinto.config;

import com.cloudkinto.common.RabbitQueueConstant;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.HashMap;
import java.util.Map;

/**
 * Author Administrator
 *
 * @Des
 * @Version
 * @Date 2021/10/14
 */
@Configuration
public class RabbitMQConfig {
    @Value("${spring.rabbitmp.host}")
    private String HOST;
    @Value("${spring.rabbitmp.port}")
    private Integer PORT;
    @Value("${spring.rabbitmp.virtual-host}")
    private String VIRTUALHOST;
    @Value("${spring.rabbitmp.username}")
    private String USERNAME;
    @Value("${spring.rabbitmp.password}")
    private String PASSWORD;
    @Value("${spring.rabbitmp.listener.simple.concurrency}")
    private int concurrency;
    @Value("${spring.rabbitmp.listener.simple.max-concurrency}")
    private int maxConcurrency;
    @Value("${spring.rabbitmp.listener.simple.acknowledge-mode}")
    private String acknowledgeMode;
    @Value("${spring.rabbitmp.listener.simple.prefetch}")
    private int prefetch;
    //    @Autowired
//    private MessageCustomConsumer listener;
    @Autowired
    private QueueService queueService;


    @Bean
    public CachingConnectionFactory connectionFactory() {
        CachingConnectionFactory connectionFactory = new CachingConnectionFactory(this.HOST, this.PORT);
        connectionFactory.setUsername(this.USERNAME);
        connectionFactory.setPassword(this.PASSWORD);
        connectionFactory.setVirtualHost(this.VIRTUALHOST);
        return connectionFactory;
    }

    @Bean
    public SimpleRabbitListenerContainerFactory rabbitListenerContainerFactory() {
        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
        factory.setConnectionFactory(connectionFactory());
        factory.setConcurrentConsumers(concurrency);
        factory.setMaxConcurrentConsumers(maxConcurrency);
        factory.setPrefetchCount(prefetch);
        return factory;
    }

    // 特定队列的工厂
    @Bean
    public SimpleRabbitListenerContainerFactory singleConsumerFactory() {
        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
        factory.setConnectionFactory(connectionFactory());
        factory.setPrefetchCount(1);
        factory.setConcurrentConsumers(1);
        factory.setMaxConcurrentConsumers(1);  // 固定单消费者
        return factory;
    }

    @Bean
    public RabbitTemplate rabbitTemplate() {
        return new RabbitTemplate(connectionFactory());
    }

    @Bean
    public RabbitAdmin rabbitAdmin() {
        return new RabbitAdmin(connectionFactory());
    }
}
